import { addFeeApi, modifyAddFeeApi } from "@/api/financialManagement/receivable/additionalExpense";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { Ref } from "vue";
export default function OperationHook(state: {
  formState: AdditionalExpense.AddFee;
  formDataRef: Ref<any>;
  emit: any;
  loading: Ref<boolean>;
  type: Ref<number>;
  passData: Ref<AdditionalExpense.ListRows>;
}) {
  const { formState, formDataRef, emit, loading, type, passData } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const onFinish = () => {
    const params = {
      ...formState,
      id: type.value === 1 ? undefined : passData.value.id
    };
    loading.value = true;

    let requestFn;

    if (type.value === 1) {
      requestFn = addFeeApi;
    } else {
      requestFn = modifyAddFeeApi;
    }
    requestFn(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields,
    validateZhInputHandle
  };
}
